//
//  ViewController.m
//  MVVM+RAC
//
//  Created by bopeng on 16/5/13.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "LoginViewModel.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "Account.h"
#import "NetWorkViewController.h"

@interface ViewController ()

@property (nonatomic, strong) LoginViewModel *loginViewModel;

@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation ViewController

- (LoginViewModel *)loginViewModel {
    return _loginViewModel ? : (_loginViewModel = [[LoginViewModel alloc] init]);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self bindModel];
    self.pwdTextField.secureTextEntry = YES;
    
    [self animationDismissLauch];
}

#pragma mark - 动画消失启动页
- (void)animationDismissLauch {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    CGSize viewSize = keyWindow.bounds.size;
    NSString *viewOrientation = @"Portrait";    //横屏请设置成 @"Landscape"
    NSString *launchImage = nil;
    
    NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary* dict in imagesDict)
    {
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        
        if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]])
        {
            launchImage = dict[@"UILaunchImageName"];
        }
    }
    
    UIImageView *launchView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:launchImage]];
    launchView.frame = keyWindow.bounds;
    launchView.contentMode = UIViewContentModeScaleAspectFill;
    [keyWindow addSubview:launchView];
    
    [UIView animateWithDuration:2.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        launchView.alpha = 0.0f;
        launchView.layer.transform = CATransform3DScale(CATransform3DIdentity, 1.2, 1.2, 1);
    } completion:^(BOOL finished) {
        [launchView removeFromSuperview];
    }];
}


/**
 *  绑定ViewModel
 */
- (void)bindModel {
    
    // 给模型的属性绑定信号
    RAC(self.loginViewModel.account, account) = _accountTextField.rac_textSignal;
    RAC(self.loginViewModel.account, pwd) = _pwdTextField.rac_textSignal;
    
    // 给登录按钮绑定信号
    RAC(self.loginBtn, enabled) = self.loginViewModel.enableLoginSignal;
    
    
    // 监听登录成功的信号
    self.loginViewModel.loginSubject = [RACSubject subject];
    [self.loginViewModel.loginSubject subscribeNext:^(id x) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        NetWorkViewController *netVC = [sb instantiateViewControllerWithIdentifier:NSStringFromClass([NetWorkViewController class])];
        [self.navigationController pushViewController:netVC animated:YES];
    }];
    
    // 监听登录按钮点击事件
    [[_loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        [self.accountTextField resignFirstResponder];
        [self.pwdTextField resignFirstResponder];
        
        // 执行登录事件
        [self.loginViewModel.loginCommand execute:nil];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
